package control.networking;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import model.*;
import model.Player.PlayerTurn;

/**
 * PlayerTurnQueue manages the sequence of turns in the game.
 * @author Kevin Luikens
 *
 */
public class PlayerTurnQueue {
	private BlockingQueue<Player> queue;
	private List<Player> players;
	
	public PlayerTurnQueue( List<Player> players, int size ) {
		if (players == null)
			System.out.println("Your player list in queue is null");
		System.out.println(players);
		queue = new ArrayBlockingQueue<Player>( size );
		this.players = players;
		populateQueue();
	}
	
	private void populateQueue() {
		for( Player player : players ) {
			if (player == null)
				System.out.println("your player in queue is NULL");
			queue.add( player );
		}
	}
	
	/**
	 * Grabs Player at the front of queue, returns said
	 * Player, and throws back to the end of the queue.
	 * @return
	 */
	public Player getNextPlayer() {
		Player head = queue.poll(); 
		queue.add( head );
		queue.peek().setTurnPhase(PlayerTurn.START);
		System.out.println(queue.peek());
		return queue.peek();
	}
	
	
}
